package com.yehun.validator;

import com.yehun.enums.BaseEnum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class NotEnumConstraintValidator implements ConstraintValidator<NotEnum, BaseEnum> {

    private int[] exclude;

    public void initialize(NotEnum notEnum) { this.exclude = notEnum.exclude(); }

    @Override
    public boolean isValid(BaseEnum baseEnum, ConstraintValidatorContext constraintValidatorContext) {
        if (baseEnum == null) {
            return false;
        }
        if (this.exclude != null && this.exclude.length > 0) {
            for (int value : this.exclude) {
                if (baseEnum.getValue().equals(value)) {
                    return false;
                }
            }
        }
        return true;
    }


}